const Koa = require("koa");
const Router = require("koa-router");
const Static = require("koa-static");
const bodyParser = require("koa-bodyparser");
const routes = require("./routers/api");

const config = require("./routers/config/default");
const { Mysql, addSql, xiuSql, remSql } = require("./routers/db/db");
const { test } = require("./routers/middleware/test");
// const demoRouter = require("./router/demo")
// 实例化koa
const app = new Koa();
// 实例化koa-Router
const router = new Router();
app.use(test);
app.use(bodyParser());
// router.use("/getDemo", demoRouter)
app.use(Static(__dirname + "/public"));
app.use(async (ctx, next) => {
  ctx.set("Access-Control-Allow-Origin", "*");
  ctx.set("Access-Control-Allow-Headers", "Content-Type");
  ctx.set("Access-Control-Allow-Methods", "POST,GET, OPTIONS, PUT");
  await next();
});
//

router.get("/", async (ctx) => {
  // 查找数据
  let data = await Mysql("student");
  // 添加数据
  // let data = await addSql("student",{Sid:16,Sname:'赵孟凡',Sage:'1990-01-01 00:00:00',Ssex:'男'})
  // 删除数据
  // let data = await remSql("student", { Sid: 14 });
  // 修改数据
  // let data = await xiuSql("student",{Sname:"赵孟凡",Sage:"2001-04-18 00:00:00",Ssex:"女"},{Sid:14});
  ctx.body = {
    code: 1,
    data: data,
    mesg: "ok",
  };
});

// 配置路由地址localhost:4000/indexRouter/api/koaRouter
router.use("/routes/api/index", routes);
// 配置路由
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(config.port, () => {
  console.log("server started on 3307");
});
// generator
